#include "Framework/Timer.h"


namespace tyro
{
	namespace Framework
	{
		Timer::Timer()
		{
			/* Get the CPU clock frequency */
			QueryPerformanceFrequency( &ticksPerSecond );

			/* Set the first reading */
			QueryPerformanceCounter( &previousTime );

			/* Start the timer */
			GetTimeStep();

			/* Initiliase the timer variables */
			this->StartMs = this->CurrentMs;
			this->ElapsedMs = 0;
		}

		UINT_32 Timer::GetElapsedMs()
		{
			return ElapsedMs;
		}

		inline FLOAT_32 Timer::GetTimeStep()
		{
			
			/* Get the next time reading */
			LARGE_INTEGER time;
			QueryPerformanceCounter(&time);

			/* Calculate the elapsed time */
			LARGE_INTEGER elapsedTime;
			elapsedTime.QuadPart = time.QuadPart - previousTime.QuadPart;

			/* Calculate the update step*/
			FLOAT_32         updateStep;
			updateStep = static_cast<FLOAT_32>(static_cast<FLOAT_64>(elapsedTime.QuadPart) / static_cast<FLOAT_64>(ticksPerSecond.QuadPart));
			
			/* Set the previous time */
			previousTime.QuadPart = time.QuadPart;

			this->CurrentMs = (FLOAT_32)( time.QuadPart * 1000 / ticksPerSecond.QuadPart );
			this->ElapsedMs = this->CurrentMs - this->StartMs;

			return updateStep;
		}


	}
}
